﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using RATSharpW7.ViewModel;
using System.Windows.Navigation;
using RATSharp.Model;
using Coding4Fun.Toolkit.Controls;
using Microsoft.Phone.Shell;

namespace RATSharpW7.View
{
    /// <summary>
    /// Simple reservation view. No edit actions, every 
    /// editable property is disabled.
    /// </summary>
    public partial class ViewReservation : PhoneApplicationPage
    {

        bool isNewPageInstance = false;
        PeriodViewModel model;

        public ViewReservation()
        {
            InitializeComponent();

            isNewPageInstance = true;
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {

            if (e.NavigationMode !=
                  System.Windows.Navigation.NavigationMode.Back)
            {
                State["PeriodViewModel"] = model;
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (isNewPageInstance)
            {
                if (model == null)
                {
                    if (State.ContainsKey("PeriodViewModel"))
                    {
                        model = State["PeriodViewModel"]
                                           as PeriodViewModel;
                    }
                    else
                    {
                        var period = NavigationContext.GetData<period>();
                        model = new PeriodViewModel(period);
                    }
                }
                DataContext = model;
            }
            isNewPageInstance = false;
            initActionBar();
        }

        private void initActionBar()
        {
            this.ApplicationBar = new ApplicationBar();
            this.ApplicationBar.IsVisible = true;
            this.ApplicationBar.Opacity = 1;
            ApplicationBarMenuItem viewObjectItem = new ApplicationBarMenuItem();
            viewObjectItem.Text = Application.Current.Resources["ViewObjectLabel"] as string;
            viewObjectItem.Click += new EventHandler(viewObjectButton_Click);
            this.ApplicationBar.MenuItems.Add(viewObjectItem);
        }

        private void viewObjectButton_Click(object sender, EventArgs e)
        {
            model.loadObject(model.DataSource.reservation.objectId, new EventHandler<PeriodViewModel.LoadObjectEventArgs>(objectLoaderHandler));
        }

        private void objectLoaderHandler(object sender, PeriodViewModel.LoadObjectEventArgs e)
        {
            if (e.succesfull)
            {
                NavigationService.Navigate("View", typeof(SimpleObjectProfile), e.obj);
            }
        }
    }
}